package com.admin.dbdao;

import com.data.dbdao.BaseDao;
import com.user.bean.App;
import com.user.bean.AppIcon;
import com.user.bean.AppRefuse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 软件信息管理Dao
 *
 * @author:HingLo
 * @create 2017-09-21 10:21
 **/
@Repository
@Transactional
public class SoftwareExamineDao extends BaseDao {
    private static Logger logger = Logger.getLogger(SoftwareExamineDao.class);

    public List<?> findSoftwareDao(int state, String fid) {
        String hql = "SELECT app.id ,app.uid ,app.name ,app.ctime ,menu.name ,app.md5  FROM App as app,Menu as menu where app.menuid=menu.id and app.state=? and menu.fid=?";
        return super.findMassageDao(hql, state, fid);
    }

    /**
     * 根据图片的id与类型查找图片信息
     * * @param rid
     *
     * @param type
     * @return
     */
    public List<AppIcon> findSoftwareIconService(String rid, String type) {
        String hql = "from AppIcon where rid=? and  types=?";
        return (List<AppIcon>) super.findMassageDao(hql, rid, type);
    }

    /**
     * 根据软件id查找软件信息
     *
     * @param rid
     * @return
     */
    public String findApkDao(String rid) {
        String hql = "from App where id=?";
        List<App> list = (List<App>) super.findMassageDao(hql, rid);
        return list.size() > 0 ? list.get(0).getMd5() : null;
    }

    /**
     * 更新软件，状态
     *
     * @param id
     * @return
     */
    public boolean updateSoftwareDao(String id, int state) {
        String hql = "update App as app set app.state=? where app.id=?";
        return super.updateOneMssageDao(hql, state, id);
    }

    /**
     * 不通过该软件的审核
     *
     * @param state
     * @param appRefuse
     * @return
     */
    public boolean updateSoftwareRefuseDao(int state, AppRefuse appRefuse) {
        if (updateSoftwareDao(appRefuse.getRid(), state)) {
            return super.saveMassageDao(appRefuse);
        }

        return false;
    }
}
